package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.alipay.PayVo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Slf4j
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    /**
     * 在支付宝创建的应用的id
     */
    private   String app_id = "2021000119643284";

    /**
     * 商户私钥，您的PKCS8格式RSA2私钥
     */

    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCDx8K4POJ0AJwPo+Prm7LkXiVEJ+RJ3hTEa4NW/SaHs45H4pgTrMk89Zi+1CS8qN5DOi9nNz12gdtyyz3PAVcul/wA3UalWC3DExRikKxntEtnh1679V7XuDulT7zNvlh6zhGtdBbULI6MMtJBXTLGT9Aj5ZGQXo75ulo3oe4I9t0Ph9BHTdV7c95QXpv5HAk1j41sbfIw2XabPwS68NPw0BUuAdGilr4W5ebn8Fim46rT6Ukjxp5CNWQTZP/oSlFlxbRZ1rN9cj/YQnnNfxitJ2Yv50/jnYprZGtuIxARQj5HRuSLaJ7o2FzDBKLqcPzQ9UhSAwe8S0buxI0qR8VrAgMBAAECggEAVLAa1hXinhQKNw7nGXINDC/omR1d247KH9TMKio0Cb99r3rSqso+5VscF370QP3xIJkS/60DZdZcNjCg/xhoc7weqluJ3BaICjaHwjQhZ9/5GYZofjhZBB8Uzc8pkbtC12D5AVR6m04rKbQ7ZOBFEicWpwsQegwarChzdA8vjzhSfx92e1sdnd39+kI7rp6yZahNrLyBiPV+Nkx24eeZguuKBfMKuISxsENODmW9/MgjruCNq8byW1kTfyQVbmgWL+mfCCK/6JeF2PFnd4qpk29Y2VoM+jy/ulTlY/AF/BZ8K5xd8A2mHJ8ee8fsitg/1k/d/q1RhaS67eoQTyelwQKBgQC8bnH6uVKte3W1rXBX20KxGhBL736nq4tmbc98dcdCdEsxShnlOCF/WadLUU/g/X86VVNg041F5fSWL3ttLqBKcbczFJmoN/HzjMe5EdkE0eDpMZuG8hHokJEttfVZZquyY59dDnloqlLhHlfkjJJXiKhrHBAtuW9qwxz5eLFx4QKBgQCzCODxdIPy3RArcW8Nt3a2HlAr1jxw/lquQ2kklW33BwThpPb2lmheth1gL9mYV3ASCyg4mxSmTPnJSaXFsee0yWyx2YnM9GIwg5+Pgt7ZJUXqSHnt7zHeN8uNwOX+/12McRsAZrVk4BZjn1XmoXrfRzhZE8S3T3M01LLKQFF4ywKBgQCIbA3pUDMjlrbqCd/EO+JVBmG2GHbCPdh3GBmYKk1RN5EY5mVJJxJZeTc9uGj0o5NxUEVFXORzLsGd2lKqAY/bVekk+k0lFRv/EHWo79sHOkxziI7pbGi6z6/MQGiZiVPVm3rNjKUpuea3RZisx3lMFoyYv4CXGiIoUYqws0kFAQKBgAYNvs1rSyrjHrDZXQ/xrCnsTHmu/Mw1TYum6984zkcUDa4zjcY4v760Gbou5ASqsDdr+Sgw1jnpUGjl/Jz6rwMF7I729BniCY/qY3LO0go1oG+Tk8vf0qfY6yEX7K76o4ckK+2Pi4KlsRwdPyJVQ3pZRiZ87KHmJdcbehAsrJSRAoGBAKgkc0ReSM/Orxc06fQ7qLKXvsqscJDNyirponLr6EnWDRhd0gkzLaOfBiHdrP6qZWWeW6XT6YylqEjBQ7r4MWyLXki+kn3YAIy6OL2OeUQEAl5Cl9LWXAkXaIsRvERQXTEJvv1kf/9pXWKWsS2Bk+lZP6aCtwkbsTmfCxep+6Zp";
    /**
     * 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
     */
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm/+WJv8vxa79X3hswxyfq8U4yQakhwweosEZ9StFXWtBiK1dCEDtGJk8ZEM+6DCq0BJlqLCAsYvtSv8lMpPBEygFO01sDGfmivd08jN9xGC03VwWXsXeCOcxbjF6FjwNcUvHNrP7md/6HNZNUdZxUHh7Aey1TWyX5bOhrsD53GI6cBEGegDdYBCjjRqPbZALVe8y1A/G8MVnzzYFowYBlyCXfncFH5fZKpMrEhIse1NRkvECG5JKHvGIHgWL0avW4XuF7cyvEz9kG6x14hLhgYQDs3yZ3lz0XiOVSCLoCUiafWqk6usnKqcXAlPSOJ+TnNGU3/UvoBxYdWtpkMyc3QIDAQAB";
    /**
     * 1、 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
     *  2、支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
     */
    private  String notify_url="http://kn4fkjsbtu.51xd.pub/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";

    /**
     * 1、页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
     * 2、同步通知，支付成功，一般跳转到成功页
     */
    private  String return_url="http://kn4fkjsbtu.51xd.pub/alipay.trade.page.pay-JAVA-UTF-8/return_url.jsp";;

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    /**
     * 支付宝网关； https://openapi.alipaydev.com/gateway.do
     */
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        //过期时间，自己添加的
        String timeout_express = vo.getTimeout_express();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout_express +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        //log.info("支付宝的响应==>{}",result);
        return result;

    }
}
